﻿using System;
using System.Data;
using System.Configuration;
using System.Xml;
using System.IO;
using HtmlAgilityPack;
//using Newtonsoft;
using System.Text;
using System.Net;



/// <summary>
/// Summary description for ExchangeVietComBank
/// </summary>
/// 
namespace WorldIndexInfo
{
    public class WorldIndexq : WorldIndex
    {
        public WorldIndexq()
        {
            this.LinkService = ConfigurationSettings.AppSettings["indexq"].ToString();
        }
        public WorldIndexq(string pUrl)
        {
            this.LinkService = pUrl;
        }
        #region "Method"
        public override void getInfo()
        {
           Boolean ret = true;
          
                # region "Gán Giá trị"
             
                #endregion
                XmlDocument xmldoc = new XmlDocument();
                try
                {
                    //VNS.Web.HttpWeb webGet = new VNS.Web.HttpWeb();
                    string strXML = WebTool.getWebContent(this.LinkService);
                    GetDJAIndex();
                    this.Nadaqinfo = getIndex(strXML, "NASDAQ");
                    this.Londoninfo = getIndex(strXML, "London");
                    this.JASDAQinfo = getIndex(strXML, "JASDAQ");
                    this.Russiainfo = getIndex(strXML, "Russia");
                    this.SP500info = getIndex(strXML, "S&P 500");
                    this.Hongkonginfo = getIndex(strXML, "Hong Kong");
                    this.Nikkei225info = getIndex(strXML, "Nikkei 225");
                    //this. = getIndex(strXML, "Hong Kong");

                    /*XmlReader a= new xmldoc()

                    XPathDocument doc = new XPathDocument(new XmlReader(strXML));
                    XPathNavigator nav = doc.CreateNavigator();

                   HtmlDocument doc = new HtmlDocument();
                    doc.LoadHtml(strXML);
                 doc.g
*/

                   
                }
                catch (Exception ex)
                {
                    ret = false;
                  
                }
           
             if (ret)
                    this.setNewValue();
            
        }
        #endregion

        public static Index getIndex(string strHTML, string name)
        {
            Index oResuld = new Index();
            oResuld.Value = "";
            oResuld.ChangeValue = "";
            oResuld.Local = "";
            oResuld.ChangePercent = "";
            string Local = "";
            float Value = 0;
            try
            {
                string temp = "";
                strHTML = NewStr(strHTML, name);
                strHTML = NewStr(strHTML, "<td nowrap");
                temp = StrCut(strHTML);
                if (temp != "" && temp != "&nbsp;")
                    oResuld.Value = temp;
                strHTML = NewStr(strHTML, "change");
                temp = StrCut(strHTML);
                if (temp != "" && temp != "&nbsp;")
                    oResuld.ChangeValue = temp;
                strHTML = NewStr(strHTML, "change");
                temp = StrCut(strHTML);
                if (temp != "" && temp != "&nbsp;")
                    oResuld.ChangePercent = temp.Replace("%","");
                strHTML = NewStr(strHTML, "center");
                temp = StrCut(strHTML);
                if (temp != "" && temp != "&center;")
                    oResuld.Local = temp;
            }
            catch (Exception e) { };

            return oResuld;
        }

        private static string ReadHTML(string url)
        {
            string htmlContent = "";
            try
            {
                WebRequest requestHtml = WebRequest.Create(url);
                WebResponse responseHtml = requestHtml.GetResponse();
                StreamReader r = new StreamReader(responseHtml.GetResponseStream());
                htmlContent = r.ReadToEnd();
                r.Close();
            }
            catch { };
            return htmlContent;
        }
        public static string StrCut(string str)
        {
            string kq = "";
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != ' ' && str[i] != '<' && str[i] != '%')
                    kq += str[i];
                else
                    break;
            }
            return kq;
        }
        public static string NewStr(string str, string mau)
        {
            int location = str.IndexOf(mau);
            int len = 0;
            string kq = "";
            try
            {
                for (int i = location + mau.Length; i < str.Length; i++)
                {
                    if (str[i] != '>')
                        len++;
                    else
                        break;
                }
                kq = str.Substring(location + len + mau.Length + 1);
            }
            catch (Exception e) { return kq; };
            return kq;
        }

        public void GetDJAIndex()
        {
            string strXML = WebTool.getWebContent("http://www.marketwatch.com/tools/marketsummary/marketMonitor.asp?refresh=on");

            int start = strXML.IndexOf("Dow</a><br>");
            int end = strXML.IndexOf("<",start+10);
            this.DJIAinfo.Value = strXML.Substring(start + 11, end - start - 11);
            start = strXML.IndexOf(">", end + 5);
            end = strXML.IndexOf("</span>", start);
            this.DJIAinfo.ChangeValue = strXML.Substring(start + 1, end - start-1);
            this.DJIAinfo.Value = float.Parse(this.DJIAinfo.Value).ToString("N02");
            this.DJIAinfo.ChangeValue = float.Parse(this.DJIAinfo.ChangeValue).ToString("N02");
        }

    }

    
}